import socket
import subprocess

r = socket.socket()
adress = ("127.0.0.1",8000)
r.bind(adress)
r.listen(3)

while True:
    c,adr = r.accept()
    while True:
        try:
            data = c.recv(1024)
        except Exception:
            break
        if not data:
            break
        print(str(data,"utf8"))
        obj = subprocess.Popen(str(data,"utf8"), shell=True, stdout=subprocess.PIPE)  #执行命令并返回结果
        cmd = obj.stdout.read()
        result_len = bytes(str(len(cmd)),"utf8")   #获取结果长度
        c.send(result_len)
        c.send(cmd)

r.close()